package com.example.app2024.h5;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.app2024.R;

/**
 * 作者：尚硅谷-杨光福 on 2016/7/28 11:19
 * 微信：yangguangfu520
 * QQ号：541433511
 * 作用：java和js互调
 */
public class JsCallJavaVideoActivity extends Activity
{
      
      private WebView webView;
      
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_js_call_java_video);
            
            webView = (WebView) findViewById(R.id.webview);
            WebSettings webSettings = webView.getSettings();
            //设置支持javaScript脚步语言
            webSettings.setJavaScriptEnabled(true);
            
            //支持双击-前提是页面要支持才显示
            //        webSettings.setUseWideViewPort(true);
            
            //支持缩放按钮-前提是页面要支持才显示
            webSettings.setBuiltInZoomControls(true);
            
            //设置客户端-不跳转到默认浏览器中
            webView.setWebViewClient(new WebViewClient());
            
            //设置支持js调用java
            webView.addJavascriptInterface(new AndroidAndJSInterface(), "android");
            
            //加载网络资源
            //        webView.loadUrl("http://atguigu.com/teacher.shtml");
            webView.loadUrl("file:///android_asset/RealNetJSCallJavaActivity.htm");
            //        webView.loadUrl("http://10.0.2.2:8080/assets/RealNetJSCallJavaActivity.htm");
            
      }
      
      class AndroidAndJSInterface
      {
            /**
             * 该方法将被js调用
             *
             * @param id
             * @param videoUrl
             * @param tile
             */
            @JavascriptInterface
            public void playVideo(int id, String videoUrl, String tile)
            {
                  //调起系统所有播放器
                  Intent intent = new Intent();
                  intent.setDataAndType(Uri.parse(videoUrl), "video/*");
                  startActivity(intent);
            }
      }
}
